package rta.datos.conexion
{
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import rta.datos.to.ImagenEstadistica;
	import rta.datos.to.Perfil;
	import rta.datos.to.PerfilEstadistica;

	public class ImagenEstadisticaDB
	{
		private var conn:SQLConnection;
		private var statement:SQLStatement;
		private var _callback:Function;
		
		public function ImagenEstadisticaDB(conn:SQLConnection)
		{
			this.conn = conn;
			statement = new SQLStatement();
			statement.itemClass = ImagenEstadistica;
			statement.sqlConnection = conn;
			statement.addEventListener(SQLErrorEvent.ERROR, onSQLError);
		}
		
		public function set callback(value:Function):void
		{
			_callback = value;
		}
		
		private function onSQLError(event:SQLErrorEvent):void{
			trace(event.text);
		}
		
		//-------------------------------------------------
		
		
		//--OBTENER TOP 10 DE IMAGENES REPRODUCIDAS-----------
		public function obtenerImagenesEstadistica():void{					
			statement.text = "SELECT img.id_imagen idImagen, img.nombre imagen, img.ruta_imagen rutaImagen, SUM(ir.repeticiones) vista, c.nombre nombreCarpeta " +
								"FROM imagen_reproducida ir " +
								"INNER JOIN imagen_secuencia ims ON ims.id_imagen_secuencia = ir.id_imagen_secuencia " +
								"INNER JOIN imagen img ON img.id_imagen = ims.id_imagen " +
								"INNER JOIN carpeta c ON c.id_carpeta = img.id_carpeta " +
								"GROUP BY img.id_imagen " +
								"ORDER BY vista DESC " +
								"LIMIT 10";
			statement.addEventListener(SQLEvent.RESULT, resultObtenerImagenesEstadistica);
			statement.execute();
		}
		
		private function resultObtenerImagenesEstadistica(event:SQLEvent):void{
			statement.removeEventListener(SQLEvent.RESULT, resultObtenerImagenesEstadistica);
			var result:SQLResult = statement.getResult();
			if (result != null && this._callback != null) {  
				this._callback(new ArrayCollection(result.data));
			}
		}
		//-------------------------------------
	}
}